package com.iflytek.uoamp.notify.mail.format;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.iflytek.uoamp.common.notify.obj.BaseInfo;
import com.iflytek.uoamp.common.notify.obj.CompInfo;
import com.iflytek.uoamp.common.notify.obj.Content;
import com.iflytek.uoamp.common.notify.obj.MonitorInfo;
import com.iflytek.uoamp.common.notify.obj.Notification;
import com.iflytek.uoamp.common.notify.util.NotifyUtils;

public class FormatCompMail extends FixedMailFormatter {
	
    private static final String COMPTEMPLATE = getTemplate("comp_template.html");
    private int rowCounts = 0;
    
    @Override
    public String pretty(Notification notification, List<?> contents) {
        StringBuilder data = new StringBuilder();
        StringBuilder content = new StringBuilder(getTitle(contents) + "@@" + COMPTEMPLATE);
        
        String dataTemplate = getDataTemplate(COMPTEMPLATE);
        String rowTemplate = getRowTemplate(COMPTEMPLATE);
        
        for(Object item : contents) {
            String block = fillTemplate((Content)item, rowTemplate, dataTemplate);
            data.append(block);
        }
        replace(data, "${rows}", Integer.toString(rowCounts));
        fillAllData(content, data.toString());


        //wlhu2, 添加组件联系人信息
        fillPrincipalData(content, notification);
        return content.toString();
    }

    private String fillTemplate(Content content, String rowTemplate, String dataTemplate) {
        StringBuilder result = new StringBuilder();
        List<String> quota = content.getQuota();
        MonitorInfo monitorInfo = content.getMonitorInfo();
        StringBuilder row = null;
        
        if(quota == null || monitorInfo == null) {
            return "";
        }
        
        for(String item : quota) {
            if(rowCounts == 0) {
                row = new StringBuilder(rowTemplate);
            } else {
                row = new StringBuilder(dataTemplate);
            }
            
            BaseInfo baseInfo = monitorInfo.getBaseInfo();
            CompInfo compInfo = monitorInfo.getCompInfo();
            
            replace(row, "${monitortime}", content.getMonitorTime());
            replace(row, "${componentname}", compInfo == null ? "" : compInfo.getComponentName());
            replace(row, "${ip}", baseInfo == null ? "" : baseInfo.getIP());
            replace(row, "${port}", compInfo == null ? "" : compInfo.getPort());
            replace(row, "${quota}", item);
            replace(row, "${quota.value}", NotifyUtils.getAttributeValue(content.getOutput(), item));
            if(rowCounts == 0) {
                if(StringUtils.isNotEmpty(NotifyUtils.getAttributeValue(content.getOutput(), "mailstatedes"))){
           		 replace(row, "${mailstatedes}", NotifyUtils.getAttributeValue(content.getOutput(), "mailstatedes"));
	           	}
	           	else{
	           		if(content.getOutput().contains("subject=") && content.getOutput().contains("monitorname=")){
	           			replace(row, "${mailstatedes}", content.getOutput().substring(content.getOutput().indexOf((char)31)+1, content.getOutput().length()));
	           		}
	           		else{
	           			replace(row, "${mailstatedes}", "");
	           		}
	           	}
            }
            result.append(row);
            ++rowCounts;
        }
        
        return result.toString();
    }
}
